﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.ETL.TaskProxy
{
    public static class TaskFileManager
    {
        public static string TaskDirectory = "";

        /// <summary>
        /// 保存任务文件
        /// </summary>
        /// <param name="task"></param>
        public static void Save(Task task)
        {
            string filePath = string.Format("{0}{1}.xml", TaskDirectory, task.Id);
            FileInfo fileInfo = new FileInfo(filePath);

            if(!fileInfo.Directory.Exists)
            {
                fileInfo.Directory.Create();
            }

            XmlDocument xmlDoc = new XmlDocument();
            XmlElement xTask = xmlDoc.CreateElement("Task");

            XmlElement xId = xmlDoc.CreateElement("Id");
            xId.InnerText = task.Id.ToString();
            xTask.AppendChild(xId);

            XmlElement xService = xmlDoc.CreateElement("Service");
            xService.InnerText = task.ServiceId.ToString();
            xTask.AppendChild(xService);

            XmlElement xSource = xmlDoc.CreateElement("Source");
            xSource.InnerText = task.ServiceId.ToString();
            xTask.AppendChild(xSource);

            XmlElement xRunModes = xmlDoc.CreateElement("RunModes");
            xTask.AppendChild(xRunModes);
            for (int index = 0; index < task.RunModes.Count; index++)
            {
                TaskRunMode runMode = task.RunModes[index];

                XmlElement xRunMode = xmlDoc.CreateElement("RunMode");

                XmlElement xMode = xmlDoc.CreateElement("Mode");
                xMode.InnerText = runMode.Mode.ToString();
                xRunMode.AppendChild(xMode);

                if(runMode.Mode == CPPEI.ETL.Domain.DEF.TaskRunMode.Timer)
                {
                    XmlElement xTimerBeginTime = xmlDoc.CreateElement("TimerBeginTime");
                    xTimerBeginTime.InnerText = runMode.TimerBeginTime.Value.ToString();
                    xRunMode.AppendChild(xTimerBeginTime);

                    XmlElement xTimerInterval = xmlDoc.CreateElement("TimerInterval");
                    xTimerInterval.InnerText = runMode.TimerInterval.Value.ToString();
                    xRunMode.AppendChild(xTimerInterval);

                    XmlElement xTimerIntervalMode = xmlDoc.CreateElement("TimerIntervalMode");
                    xTimerIntervalMode.InnerText = runMode.TimerIntervalMode.Value.ToString();
                    xRunMode.AppendChild(xTimerIntervalMode);
                }
                else if (runMode.Mode == CPPEI.ETL.Domain.DEF.TaskRunMode.Alarm)
                {
                    XmlElement xAlarmMode = xmlDoc.CreateElement("AlarmMode");
                    xAlarmMode.InnerText = runMode.AlarmMode.Value.ToString();
                    xRunMode.AppendChild(xAlarmMode);

                    XmlElement xAlarmClock = xmlDoc.CreateElement("AlarmClock");
                    xAlarmClock.InnerText = runMode.AlarmClock.Value.ToString();
                    xRunMode.AppendChild(xAlarmClock);
                }
                else if (runMode.Mode == CPPEI.ETL.Domain.DEF.TaskRunMode.Event)
                {
                    XmlElement xEventName = xmlDoc.CreateElement("EventName");
                    xEventName.InnerText = runMode.EventName;
                    xRunMode.AppendChild(xEventName);
                }

                xRunModes.AppendChild(xRunMode);
            }

            xmlDoc.AppendChild(xTask);
        }

        /// <summary>
        /// 删除任务文件
        /// </summary>
        /// <param name="task"></param>
        public static void Delete(Task task)
        {
            string filePath = string.Format("{0}{1}.xml", TaskDirectory, task.Id);

            File.Delete(filePath);
        }

        /// <summary>
        /// 获取数据文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static Task Load(string filePath)
        {
            FileInfo fileInfo = new FileInfo(filePath);

            if (!fileInfo.Directory.Exists)
            {
                fileInfo.Directory.Create();
            }

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);

            Task task = new Task();

            XmlNode xTask = xmlDoc.SelectSingleNode("./Task");

            XmlNode xId = xTask.SelectSingleNode("./Id");
            task.Id = new Guid(xId.InnerText);

            XmlNode xService = xTask.SelectSingleNode("./Service");
            task.ServiceId = new Guid(xService.InnerText);

            XmlNode xSource = xTask.SelectSingleNode("./Source");
            task.SourceId = new Guid(xSource.InnerText);

            XmlNodeList xRunModes = xTask.SelectNodes("./RunModes/RunMode");
            for (int index = 0; index < xRunModes.Count; index++)
            {
                XmlNode xRunMode = xRunModes[index];

                TaskRunMode runMode = new TaskRunMode();

                XmlNode xMode = xRunMode.SelectSingleNode("./Mode");
                runMode.Mode = Convert.ToInt32(xMode.InnerText);

                if (runMode.Mode == CPPEI.ETL.Domain.DEF.TaskRunMode.Timer)
                {
                    XmlNode xTimerBeginTime = xRunMode.SelectSingleNode("./TimerBeginTime");
                    runMode.TimerBeginTime = Convert.ToDateTime(xTimerBeginTime.InnerText);

                    XmlNode xTimerInterval = xRunMode.SelectSingleNode("./TimerInterval");
                    runMode.TimerInterval = Convert.ToInt64(xTimerInterval.InnerText);

                    XmlNode xTimerIntervalMode = xRunMode.SelectSingleNode("./TimerIntervalMode");
                    runMode.TimerIntervalMode = Convert.ToInt32(xTimerIntervalMode.InnerText);
                }
                else if (runMode.Mode == CPPEI.ETL.Domain.DEF.TaskRunMode.Alarm)
                {
                    XmlNode xAlarmMode = xRunMode.SelectSingleNode("./AlarmMode");
                    runMode.AlarmMode = Convert.ToInt32(xAlarmMode.InnerText);

                    XmlNode xAlarmClock = xRunMode.SelectSingleNode("./AlarmClock");
                    runMode.AlarmClock = Convert.ToDateTime(xAlarmClock.InnerText);
                }
                else if (runMode.Mode == CPPEI.ETL.Domain.DEF.TaskRunMode.Event)
                {
                    XmlNode xEventName = xRunMode.SelectSingleNode("./EventName");
                    runMode.EventName = xEventName.InnerText;
                }

                task.RunModes.Add(runMode);
            }

            return task;
        }

        /// <summary>
        /// 扫描目录获取所有的项目文件列表
        /// </summary>
        /// <returns></returns>
        public static List<TaskFile> Scan()
        {
            List<TaskFile> taskFiles = new List<TaskFile>();

            string[] files = Directory.GetFiles(TaskDirectory);
            for (int index = 0; index < files.Length; index++)
            {
                FileInfo fileInfo = new FileInfo(files[index]);
                TaskFile tFile = new TaskFile();
                tFile.Id = new Guid(fileInfo.Name.Replace(fileInfo.Extension, string.Empty));
                tFile.ModifiedTime = fileInfo.CreationTime;

                taskFiles.Add(tFile);
            }

            return taskFiles;
        }
    }
}
